package com.yuyou.fn.educational.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.constant.JobNatureEnum;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.EnumUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.ClassExpandDetail;
import com.yuyou.fn.educational.mapper.ClassExpandDetailMapper;
import com.yuyou.fn.educational.service.IClassExpandDetailService;
import com.yuyou.fn.educational.service.ICourseClassService;
import com.yuyou.fn.educational.vo.ClassExpandDetailVo;
import com.yuyou.fn.educational.vo.ClassExpandQueryVo;
import com.yuyou.fn.educational.vo.CourseClassQueryVo;
import com.yuyou.fn.platform.permission.DataPermission;
import groovy.transform.Synchronized;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * ClassExpandDetail 表数据服务层接口实现类
 *
 */
@Service
public class ClassExpandDetailServiceImpl extends SuperServiceImpl<ClassExpandDetailMapper, ClassExpandDetail,Long> implements IClassExpandDetailService {

    @Resource
    private ICourseClassService courseClassService;

    @Override
    public List<ClassExpandDetailVo> findClassExpandDetail(ClassExpandQueryVo classExpandQueryVo, CourseClassQueryVo courseClassQueryVo, List<Long> busTeamIds, int start, int size) {
        return autoMapper.findClassExpandDetail(classExpandQueryVo,courseClassQueryVo,busTeamIds,start,size);
    }

    @Override
    public Integer countClassExpandDetail(ClassExpandQueryVo classExpandQueryVo, CourseClassQueryVo courseClassQueryVo, List<Long> busTeamIds) {
        return autoMapper.countClassExpandDetail(classExpandQueryVo,courseClassQueryVo,busTeamIds);
    }

    @Override
    public Page<ClassExpandDetailVo> findClassExpandDetail(ClassExpandQueryVo classExpandQueryVo, CourseClassQueryVo courseClassQueryVo, DataPermission dataPermission, int pageNo, int pageSize) {
        if (courseClassQueryVo.getLessStartCourseTime()!=null){
            courseClassQueryVo.setLessStartCourseTime(TimeUtils.changeOneDateLastTime(courseClassQueryVo.getLessStartCourseTime()));
        }
        if (courseClassQueryVo.getAreaTeamId()==null){
            throw new BusException("区域id必传");
        }
        Page<ClassExpandDetailVo> page=new Page<>(pageNo,pageSize);
        if (pageNo==0){
            pageNo=1;
        }
        List<ClassExpandDetailVo> classExpandDetailVoList=this.findClassExpandDetail(classExpandQueryVo,courseClassQueryVo,dataPermission.getBusTeamIds(),(pageNo-1)*pageSize,pageSize);
        Integer total=this.countClassExpandDetail(classExpandQueryVo,courseClassQueryVo,dataPermission.getBusTeamIds());
        page.setTotal(total);
        page.setRecords(classExpandDetailVoList);
        return page;
    }


    @Override
    public SXSSFWorkbook exportClassExpandDetail(ClassExpandQueryVo classExpandQueryVo,CourseClassQueryVo courseClassQueryVo, DataPermission dataPermission) {

        Page<ClassExpandDetailVo> page = this.findClassExpandDetail(classExpandQueryVo,courseClassQueryVo ,dataPermission , 1, 20000);

        List<ClassExpandDetailVo> list=page.getRecords();

        return classExpandDetail2Excel(list);
    }

    public static SXSSFWorkbook classExpandDetail2Excel(List<ClassExpandDetailVo> classExpandDetailVoList){
        SXSSFWorkbook excel = new SXSSFWorkbook();
        SXSSFSheet sheet = excel.createSheet("班级录入情况列表");
        SXSSFRow firstRow = sheet.createRow(0);
        String[] titles = new String[]{"期数", "班级编号","班级名称", "业务组","开课日期","开课时间","学科", "年级", "教师","任教属性","录入状态"};
        for (int i = 0; i < titles.length; i++) {
            firstRow.createCell(i).setCellValue(titles[i]);
        }
        int rowNum=1;
        if (classExpandDetailVoList!=null){
            for (ClassExpandDetailVo classExpandDetailVo:classExpandDetailVoList) {
                int cellNum = 0;
                SXSSFRow row = sheet.createRow(rowNum++);

                SXSSFCell cell = row.createCell(cellNum++);
                cell.setCellValue(classExpandDetailVo.getPeriodName());

                cell = row.createCell(cellNum++);
                cell.setCellValue(classExpandDetailVo.getClassNo());

                cell = row.createCell(cellNum++);
                cell.setCellValue(classExpandDetailVo.getClassName());

                cell = row.createCell(cellNum++);
                cell.setCellValue(classExpandDetailVo.getBusTeamName());

                cell = row.createCell(cellNum++);
                cell.setCellValue(classExpandDetailVo.getStartCourseTime()==null?"":Utils.formatDate(classExpandDetailVo.getStartCourseTime(),"yyyy-MM-dd"));

                cell = row.createCell(cellNum++);
                cell.setCellValue(StringUtils.defaultString(classExpandDetailVo.getStudyingTime()));

                cell = row.createCell(cellNum++);
                cell.setCellValue(classExpandDetailVo.getGradeName());


                cell = row.createCell(cellNum++);
                cell.setCellValue(classExpandDetailVo.getSubjectName());


                cell = row.createCell(cellNum++);
                cell.setCellValue(classExpandDetailVo.getTeacherName());

                cell = row.createCell(cellNum++);
                cell.setCellValue(EnumUtils.getName(JobNatureEnum.values() ,classExpandDetailVo.getJobNature()));

                cell = row.createCell(cellNum++);
                cell.setCellValue(classExpandDetailVo.getIntoState()==1?"已录入":"未录入");
            }
        }
        return excel;
    }


    @Override
    @Transactional
    @Synchronized
    public void updateClassState(List<Long> classIds, Integer changeState,Integer intoState,Integer onlineState,Integer isRecommend) {
        if (isRecommend!=null){
            courseClassService.recommendCourseClass(classIds,isRecommend);
        }
        if(changeState==null && intoState==null && onlineState==null && isRecommend==null){
            throw new BusException("至少传入一个状态变更");
        }
        classIds=new ArrayList<>(classIds);
        List<ClassExpandDetail> classExpandDetailList=this.findByIds(classIds);
        List<Long> changeClassIds = BeanUtils.toList(classExpandDetailList, "classId");
        if (Utils.checkListNotEmpty(changeClassIds)){
            Example<ClassExpandDetail> classExpandDetailExample=ClassExpandDetail.createExample();
            classExpandDetailExample.createCriteria().andIn("classId",changeClassIds);
            ClassExpandDetail update=new ClassExpandDetail();
            update.setChangeState(changeState);
            update.setIntoState(intoState);
            update.setOnlineState(onlineState);
            update.setUpdateTime(TimeUtils.now());
            this.updateSelectiveByExample(update,classExpandDetailExample);
            classIds.removeAll(changeClassIds);
        }
        if (Utils.checkListNotEmpty(classIds)){
            List<ClassExpandDetail> classExpandDetails=new ArrayList<>();
            for (Long classId:classIds){
                ClassExpandDetail classExpandDetail=new ClassExpandDetail();
                classExpandDetail.setChangeState(changeState);
                classExpandDetail.setIntoState(intoState);
                classExpandDetail.setOnlineState(onlineState);
                classExpandDetail.setCreateTime(TimeUtils.now());
                classExpandDetail.setUpdateTime(TimeUtils.now());
                classExpandDetail.setClassId(classId);
                classExpandDetails.add(classExpandDetail);
            }
            this.insertBatch(classExpandDetails);
        }


    }
}